﻿using HookupAppForm.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HookupAppForm.MyUserControl
{
    public partial class UserControlBlock : UserControl
    {
        private HKP_BlockDto block { get; set; }
        private HKP_ToolDto tool { get; set; }
        private APIClient client;
        public delegate void MouseEnterDelegate(object sender, EventArgs e);
        public event MouseEnterDelegate mouseEnterEvent;

        public delegate void MouseLeaveDelegate(object sender, EventArgs e);
        public event MouseLeaveDelegate mouseLeaveEvent;

        public delegate void MouseHoverDelegate(object sender, EventArgs e);
        public event MouseHoverDelegate mouseHoverEvent;

        public delegate void TakeOffDelegate(object sender, EventArgs e);
        public event TakeOffDelegate takeOffEvent;
        private int currentX { get; set; }
        private int currentY { get; set; }
        private string currentSys { get; set; }
        private double FitDistence { get; set; }
        public UserControlBlock(HKP_BlockDto _block, APIClient _client, string _sys = "", HKP_ToolDto _tool = null, double _distence =0)
        {
            InitializeComponent();
            block = _block;
            client = _client;
            currentSys = _sys;
            labelM.Text = block.BlockName;
            tool = _tool == null ? new HKP_ToolDto() : _tool;
            FitDistence = _distence;
        }

        private void UserControlHDC_Load(object sender, EventArgs e)
        {
            string url = string.Format("api/services/app/HKP_Takeoff/GetList?blockId={0}", block.Id);
            var result = client.Get(url);
            ItemsDto<HKP_TakeoffDto> res = JsonConvert.DeserializeObject<ItemsDto<HKP_TakeoffDto>>(result);
            
            if (res != null && res.result != null)
            {
                int i = 0;
                var items = res.result.items;
                if (!string.IsNullOrEmpty(currentSys))
                {
                    items = items.Where(o => o.SysName == currentSys).ToArray();
                }
                foreach (HKP_TakeoffDto item in items)
                {
                    UserControlTakeOff userControlTakeOff = new UserControlTakeOff(item, client, tool, FitDistence);
                    userControlTakeOff.BackColor = Color.Transparent;
                    //userControlTakeOff.Location = new Point(7, i * userControlTakeOff.Height);
                    userControlTakeOff.Location = new Point(Convert.ToInt32(item.XAbs), Convert.ToInt32(item.YAbs));
                    userControlTakeOff.Name = item.TakeoffNo;
                    userControlTakeOff.Size = new Size(182, 26);
                    userControlTakeOff.TabIndex = 0;
                    userControlTakeOff.BringToFront();
                    //bool fit1 = !item.Used && item.Enabled;
                    //bool fit2 = distence <= FitDistence;
                    //userControlTakeOff.ForeColor = (fit1 && fit2) ? Color.Green : Color.Red;

                    this.Controls.Add(userControlTakeOff);
                    i++;
                }
            }
            LoadTool();
        }

        private void LoadTool()
        {
            string url = string.Format("api/services/app/HKP_HDC/GetList?fabArea={0}&floor={1}", block.BlockName,block.Floor);
            var result = client.Get(url);
            ItemsDto<HKP_HDCDto> res = JsonConvert.DeserializeObject<ItemsDto<HKP_HDCDto>>(result);
            if (res != null && res.result != null)
            {
                int i = 0;
                List<Guid> toolList = new List<Guid>();
                foreach (HKP_HDCDto item in res.result.items)
                {
                    if (!toolList.Contains(item.ToolId))
                    {
                        var obj = GetTool(item.ToolId);
                        UserControlTool tool = new UserControlTool(obj);
                        tool.BackColor = Color.Transparent;
                        tool.Location = new Point(60, i * tool.Height + 100);
                        tool.Name = obj.ToolId;
                        tool.Size = new Size(120, 50);
                        tool.BringToFront();
                        this.Controls.Add(tool);
                        i++;
                        toolList.Add(item.ToolId);
                    }
                }
            }
            labelM.SendToBack();
        }

        private HKP_ToolDto GetTool(Guid id)
        {
            string url = string.Format("api/services/app/HKP_Tool/Get?Id={0}", id.ToString());
            var jsons = client.Get(url);
            var resp = Utils.SuccessHandler(jsons);
            return JsonConvert.DeserializeObject<HKP_ToolDto>(resp.ToString());
        }

        private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            FormTakeOff form = new FormTakeOff(null, client, block.Id, currentX, currentY);
            form.Show();
        }

        private void ToolStripMenuItemHDC_Click(object sender, EventArgs e)
        {
            if (takeOffEvent != null)
            {
                takeOffEvent(block, e);
            }
        }

        private void UserControlBlock_MouseEnter(object sender, EventArgs e)
        {
            if (mouseEnterEvent != null)
            {
                mouseEnterEvent(block, e);
            }
        }

        private void UserControlBlock_MouseLeave(object sender, EventArgs e)
        {
            if (mouseLeaveEvent != null)
            {
                mouseLeaveEvent(block, e);
            }
        }

        private void UserControlBlock_MouseHover(object sender, EventArgs e)
        {
            if (mouseHoverEvent != null)
            {
                mouseHoverEvent(block, e);
            }
        }

        private void ToolStripMenuItemAlt_Click(object sender, EventArgs e)
        {
            FormBlock form = new FormBlock(block, client, "", "");
            form.ShowDialog();
        }

        private void UserControlBlock_MouseMove(object sender, MouseEventArgs e)
        {
            currentX = e.X;
            currentY = e.Y;
        }
    }
}
